﻿/*LTrim(string):去除左边的空格*/
function LTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(0)) != -1) {
        var j = 0, i = s.length;
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {
            j++;
        }
        s = s.substring(j, i);
    }
    return s;
}


/*RTrim(string):去除右边的空格*/
function RTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
        var i = s.length - 1;
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {
            i--;
        }
        s = s.substring(0, i + 1);
    }
    return s;
}


/*Trim(string):去除前后空格*/
function Trim(str) {
    return RTrim(LTrim(str));
}

/*英文限制*/
function C_EN(str){
    RegExpPtn = /^[A-Za-z]+$/;
    if (!RegExpPtn.test(str)) { return false; } else { return true; }
}

/*中文限制*/
function C_CN(str) {
        RegExpPtn = /^[\u4e00-\u9fa5]+$/;
        if (!RegExpPtn.test(str)) { return false; } else { return true; }
}

/*实数限制*/
function C_FLOAT(str) {
    RegExpPtn = /^(\+|-)?\d+($|\.\d+$)/;
    if (!RegExpPtn.test(str)) { return false; } else { return true; }
}

/*整数限制*/
function C_INT(str) {
    RegExpPtn = /^[0-9]*$/;
    if (!RegExpPtn.test(str)) { return false; } else { return true; }
}

/* $ 获取指定对象
@element 对象名*/
function $(element) {
    if (arguments.length > 1) {
        for (var i = 0, elements = [], length = arguments.length; i < length; i++)
            elements.push($(arguments[i]));
        return elements;
    }
    if (typeof element == 'string')
        element = document.getElementById(element);
    return element;
}

/* 检查是否开启COOKIES*/
function CookieEnable() {
    var result = false;
    if (navigator.cookiesEnabled)
        return true;
    document.cookie = "testcookie=yes;";
    var cookieSet = document.cookie;
    if (cookieSet.indexOf("testcookie=yes") > -1)
        result = true;
    document.cookie = "";
    return result;
}

/* 设置COOKIES*/
function setCookie(name, value) {
    var today = new Date();
    var expires = new Date();
    expires.setTime(today.getTime() + 1000 * 60 * 60 * 6);
    document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString();
    today = null;
    expires = null;
}

/* 获取COOKIES*/
function getCookie(Name) {
    var search = Name + "=";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) {
                end = document.cookie.length;
            }
            return unescape(document.cookie.substring(offset, end));
        }
        else {
            return ("");
        }
    } else {
        return ("");
    }
    search = null;
}


/* AJAX封装*/
function Ajax(url, pars, method, onComplete, onWait, asynchronous) {
    var xmlHttp;
    if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }

    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            onComplete(xmlHttp);
        }
        else {
            onWait();
        }
    };

    //默认用get提交  
    if (method.toLowerCase() == "get") {
        url = url + "?" + pars;
        xmlHttp.open("GET", url, asynchronous);
        xmlHttp.setRequestHeader("If-Modified-Since", "0");
        xmlHttp.send(null);
    }
    else {
        xmlHttp.open("POST", url, asynchronous);
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttp.send(pars);
    }
}

/* 获取指定的URL参数值
@name   参数名
*/
function Request(name) {
    var GetUrl = this.GetUrl();
    var Plist = new Array();
    if (GetUrl.indexOf('?') > 0) {
        Plist = GetUrl.split('?')[1].split('&');
    }
    else if (GetUrl.indexOf('#') > 0) {
        Plist = GetUrl.split('#')[1].split('&');
    }
    if (GetUrl.length > 0) {
        for (var i = 0; i < Plist.length; i++) {
            var GetValue = Plist[i].split('=');
            if (GetValue[0].toUpperCase() == name.toUpperCase()) {
                return GetValue[1];
                break;
            }
        }
        return;
    }
}

/* 禁止浏览器的Javascript错误提示 */
function CloseError() {
    window.onerror = function() { return true; };
}

function AddOption(Text, Value, Element) {
    var Text, Value, TemObj = document.getElementById(Element); //获取列表对象
    var TemOption = new Option(Text, Value); //定义一个选项对象
    //TemOption.selected = "selected"; //设定新建的项为被选项
    TemObj[TemObj.length] = TemOption; //添加到列表
}

/*
功能:给表格添加一行;
参数:[必需的参数 tableid,表格的id][可选参数2:一个包含单元格内容的数组]
要求:必需是一个已存在一行以上的表格;
*/
function addrow(tableid) {
    //获取表格对象
    var thistable = document.getElementById(tableid);
    //获取列数
    var colssize = thistable.rows[0].cells.length;
    //创建一个临时数组
    var thisvalue = new Array();
    //给数组添加内容
    for (i = 0; i < colssize; i++) { thisvalue[i] = typeof (arguments[1]) == 'undefined' ? ' ' : arguments[1][i] }
    //创建新行
    var newtr = thistable.insertRow(thistable.rows.length);
    //创建列
    for (i = 0; i < colssize; i++) {
        var newtd = newtr.insertCell(i);
        newtd.innerHTML = thisvalue[i];
        newtd.style.backgroundColor = "#fff";
        newtd.style.textAlign = "center";
        newtd.style.wordBreak = "break-all";
    }
}

/*
功能:删除一行
参数说明:table(可以是一个table对象或者table对象的id)
:tr(可以是一个tr对象或者该tr对象所在的table中的行数)    
*/
function delrow(table, tr) {
    var otable = typeof (table) == 'object' ? table : document.getElementById(table);
    var otr = typeof (tr) == 'object' ? tr.rowIndex : tr;
    otable.deleteRow(otr);
}



//***************操作select控件****************//

// 1.判断select选项中 是否存在Value="paraValue"的Item        
function SelectIsExitItem(objSelect, objItemValue) {
    var isExit = false;
    for (var i = 0; i < objSelect.options.length; i++) {
        if (objSelect.options[i].value == objItemValue) {
            isExit = true;
            break;
        }
    }
    return isExit;
}

// 2.向select选项中 加入一个Item        
function AddItemToSelect(objSelect, objItemText, objItemValue) {
    //判断是否存在        
    if (SelectIsExitItem(objSelect, objItemValue)) {
        //alert("该Item的Value值已经存在");
    } else {
        var varItem = new Option(objItemText, objItemValue);
        objSelect.options.add(varItem);
        //alert("成功加入");
    }
}

// 3.从select选项中 删除一个Item        
function RemoveItemFromSelect(objSelect, objItemValue) {
    //判断是否存在        
    if (SelectIsExitItem(objSelect, objItemValue)) {
        for (var i = 0; i < objSelect.options.length; i++) {
            if (objSelect.options[i].value == objItemValue) {
                objSelect.options.remove(i);
                break;
            }
        }
        //alert("成功删除");
    } else {
        //alert("该select中 不存在该项");
    }
}


// 4.删除select中选中的项    
function RemoveSelectedItemFromSelect(objSelect) {
    var length = objSelect.options.length - 1;
    for (var i = length; i >= 0; i--) {
        if (objSelect[i].selected == true) {
            objSelect.options[i] = null;
        }
    }
}

// 5.修改select选项中 value="paraValue"的text为"paraText"        
function UpdateItemToSelect(objSelect, objItemText, objItemValue) {
    //判断是否存在        
    if (SelectIsExitItem(objSelect, objItemValue)) {
        for (var i = 0; i < objSelect.options.length; i++) {
            if (objSelect.options[i].value == objItemValue) {
                objSelect.options[i].text = objItemText;
                break;
            }
        }
        //alert("成功修改");
    } else {
        //alert("该select中 不存在该项");
    }
}

// 6.设置select中text="paraText"的第一个Item为选中        
function SelectItemByValue(objSelect, objItemText) {
    //判断是否存在        
    var isExit = false;
    for (var i = 0; i < objSelect.options.length; i++) {
        if (objSelect.options[i].text == objItemText) {
            objSelect.options[i].selected = true;
            isExit = true;
            break;
        }
    }
    //Show出结果        
    if (isExit) {
        //alert("成功选中");
    } else {
        //alert("该select中 不存在该项");
    }
}

//7.设置对应的item选中
function SetOptionToSelect(objSelect, objItemValue) {
    for (var i = 0; i < objSelect.options.length; i++)
        if (objSelect.options[i].value == objItemValue) {
            objSelect.options[i].selected = true;
    }
}


function popWindow(surl) {
    var url = surl;
    try {
        var VCWindow = window.open(url, "newwindow", 'height=320, width=550, left=250,top=120, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
        if (VCWindow == null) {
            alert("不支持的操作");
        }
        else {
            VCWindow.focus();
        }
        VCWindow = null;
    }
    catch (e) {
    }
    url = null;
}

function popWindow_(surl) {
    var url = surl;
    try {
        var VCWindow = window.open(url, "newwindow1", 'height=500, width=820, left=100,top=100, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no');
        if (VCWindow == null) {
            alert("不支持的操作");
        }
        else {
            VCWindow.focus();
        }
        VCWindow = null;
    }
    catch (e) {
    }
    url = null;
}

function popWindow2(surl, w, h) {
    var url = surl;
    try {
        var VCWindow = window.open(url, "newwindow"+h, 'height=' + h + ', width=' + w + ', left=230,top=40, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no');
        if (VCWindow == null) {
            alert("不支持的操作");
        }
        else {
            VCWindow.focus();
        }
        VCWindow = null;
    }
    catch (e) {
    }
    url = null;
}


function compareDate(a, b) {
    var arr = a.split("-");
    var starttime = new Date(arr[0], arr[1], arr[2]);
    var starttimes = starttime.getTime();
    var arrs = b.split("-");
    var endtime = new Date(arrs[0], arrs[1], arrs[2]);
    var endtimes = endtime.getTime();
    if (starttimes > endtimes)//开始大于结束
    {
        return false;
    }
    else {
        return true;
    }
}

var request = {
    QueryString: function(val) {
        var uri = window.location.search;
        var re = new RegExp("" + val + "\=([^\&\?]*)", "ig");
        return ((uri.match(re)) ? (uri.match(re)[0].substr(val.length + 1)) : null);
    },
    QueryStrings: function() {
        var uri = window.location.search;
        var re = /\w*\=([^\&\?]*)/ig;
        var retval = [];
        while ((arr = re.exec(uri)) != null)
            retval.push(arr[0]);
        return retval;
    },
    setQuery: function(val1, val2) {
        var a = this.QueryStrings();
        var retval = "";
        var seted = false;
        var re = new RegExp("^" + val1 + "\=([^\&\?]*)$", "ig");
        for (var i = 0; i < a.length; i++) {
            if (re.test(a[i])) {
                seted = true;
                a[i] = val1 + "=" + val2;
            }
        }
        retval = a.join("&");
        return "?" + retval + (seted ? "" : (retval ? "&" : "") + val1 + "=" + val2);
    }
}

